package com.neusoft.neuiotms.dcom.converter.problem;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcom.dto.problem.ProblemAssignmentDTO;
import com.neusoft.neuiotms.dcom.model.problem.ProblemAssignment;
import com.neusoft.neuiotms.dcom.repository.problem.ProblemAssignmentRepository;

@Component
public class ProblemAssignmentConverter extends AbstractConverter<ProblemAssignment, ProblemAssignmentDTO> {

    @Autowired
    private ProblemAssignmentRepository problemAssignmentRepository;

    @Override
    public ProblemAssignment toModel(final ProblemAssignmentDTO dto) {
        ProblemAssignment model;
        if (dto.isNew()) {
            model = new ProblemAssignment();
        } else {
            model = this.problemAssignmentRepository.getOne(dto.getId());
        }
        model.setTaskId(dto.getTaskId());
        model.setWorkOrderNum(dto.getWorkOrderNum());
        model.setExecutorCode(dto.getExecutorCode());
        model.setExecutor(dto.getExecutor());
        model.setUpdateTime(new Date());

        return model;
    }

    @Override
    public ProblemAssignmentDTO toDTO(final ProblemAssignment model, final boolean forListView) {
        final ProblemAssignmentDTO dto = new ProblemAssignmentDTO();
        dto.setTaskId(model.getTaskId());
        dto.setWorkOrderNum(model.getWorkOrderNum());
        dto.setId(model.getId());
        dto.setExecutorCode(model.getExecutorCode());
        dto.setExecutor(model.getExecutor());
        dto.setCreateTime(model.getCreateTime());
        dto.setUpdateTime(model.getUpdateTime());

        return dto;
    }

}
